package com.mcptt.db.model;

import java.io.Serializable;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;

import com.cloudptt.api.comm.CheckData;
import com.mcptt.db.DbConst;

public class GroupMembInfo extends AddrInfo implements Serializable,Comparable<GroupMembInfo>
{
	public static final int Status_Connected = 1;
	public static final int GroupType_Disconnect = 0;

	private int status = GroupType_Disconnect;
	private String groupAddrId;
	private String addrNum;


	public GroupMembInfo(Cursor c)
	{
		ContentValues args = new ContentValues();
		DatabaseUtils.cursorRowToContentValues(c, args);
		createFromContentValue(args);
	}



	public GroupMembInfo(String groupAddrId,String addrId,String name,String addrNum,int priority) {
		// TODO Auto-generated constructor stub
		super(AddrInfo.MembType_Normal,name,addrId,priority);
		this.groupAddrId = groupAddrId;
		this.addrNum = addrNum;
	}



	public void createFromContentValue(ContentValues args) {

//		Log.debug(this.getClass(), "createFromContentValue");
		String tmp_s;

		tmp_s = args.getAsString(DbConst.COL_AddrId);
		if(tmp_s != null) {
			addrId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Type);
		if(tmp_s != null) {
			type = CheckData.parseInt(tmp_s);
		}
		tmp_s = args.getAsString(DbConst.COL_Name);
		if(tmp_s != null) {
			name = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_Attr);
		if(tmp_s != null) {
			int value = CheckData.parseInt(tmp_s);
			if(value != -1)
			{
				attr = value;
			}
		}
		tmp_s = args.getAsString(DbConst.COL_GroupId);
		if(tmp_s != null) {
			groupAddrId = tmp_s;
		}
		tmp_s = args.getAsString(DbConst.COL_MEMBER_NUM);
		if(tmp_s != null) {
			addrNum = tmp_s;
		}
	}
	/**
	 * Pack the object content value to store
	 *
	 * @return The content value representing the message
	 */
	public ContentValues getContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(DbConst.COL_AddrId, addrId);
		cv.put(DbConst.COL_Type,type);
		cv.put(DbConst.COL_Name, name);
		cv.put(DbConst.COL_Attr, attr);
		cv.put(DbConst.COL_GroupId, groupAddrId);
		cv.put(DbConst.COL_MEMBER_NUM, addrNum);

		return cv;
	}

	public boolean isSameOne(String oldAddrId)
	{
		if(this.addrId.equals(oldAddrId))
		{
			return true;
		}
		return false;
	}


	public String getGroupAddrId() {
		return groupAddrId;
	}
	public String getName() {
		return name;
	}
	public String getAddrNum() {
		return addrNum;
	}
	public int getStatus() {
		return status;
	}
	public boolean isConnected()
	{
		if(this.status == Status_Connected)
		{
			return true;
		}
		return false;
	}

	public void setStatus(boolean isConnected) {
		if(isConnected) {
			this.status = Status_Connected;
		}
		else
		{
			this.status = GroupType_Disconnect;
		}
	}

	public void setStatus(int status) {
		this.status = status;
	}
	@Override
	public int compareTo(GroupMembInfo other) {
		return  other.status - this.status; // 按年龄升序
	}
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("userType = ").append(type).append(",");
		strBuf.append("mcId = ").append(addrId).append(",");
		strBuf.append("name = ").append(name).append(",");
		strBuf.append("attr = ").append(attr).append(".");
		strBuf.append("groupNum = ").append(groupAddrId).append(",");
		strBuf.append("addrNum = ").append(addrNum).append(",");
		strBuf.append("status = ").append(status).append(".");

		return strBuf.toString();
	}
}
